package com.erp.servlet;

import com.erp.bean.Commodity;
import com.erp.bean.Entrepot;
import com.erp.bean.Inventory;
import com.erp.bean.Order;
import com.erp.service.CommodityService;
import com.erp.service.EntrepotService;
import com.erp.service.InventoryService;
import com.erp.service.OrderService;
import com.erp.service.impl.CommodityServiceImpl;
import com.erp.service.impl.EntrepotServiceImpl;
import com.erp.service.impl.InventoryServiceImpl;
import com.erp.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * @author lyb
 * 入库控制器
 */
@WebServlet("/enteybatch.action")
public class EnteyBatchServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //创建订单业务逻辑对象
        OrderService orderService = new OrderServiceImpl();
        //创建库存逻辑对象
        InventoryService inventoryService = new InventoryServiceImpl();
        //获取页面传递的库房id
        String ent = req.getParameter("entid");
        int entid = Integer.valueOf(ent);

        String ids = req.getParameter("ids");
        String[] id = ids.split(",");

        orderService.updateBatch(8, id);
        for (int i = 0; i < id.length; i++) {
            System.out.println(id[i]);
            Order order = orderService.findById(id[i]);
            int gid = order.getGid();
            int num = order.getCount();
            Inventory inventory = inventoryService.findByGidob(gid, entid);
            if (inventory == null) {
                System.out.println(555);
                Inventory invento = new Inventory(gid, num, entid);
                System.out.println(invento);
                inventoryService.add(invento);
            } else {
                inventory.setNum(inventory.getNum() + num);
                inventoryService.update(inventory);
            }
        }
        //重定向到库存列表控制器
        resp.sendRedirect("inventoryall.action");


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
